package com.op.teachingliteapp.database.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.op.teachingliteapp.bean.BaseBean;
import com.op.teachingliteapp.bean.CourseNotifBean;
import com.op.teachingliteapp.bean.SeminarBean;
import com.op.teachingliteapp.bean.UserBean;
import com.op.teachingliteapp.database.DBHelper;
import com.op.teachingliteapp.database.table.CourseNotifTable;
import com.op.teachingliteapp.database.table.SeminarTable;
import com.op.teachingliteapp.database.table.UserTable;
import com.op.teachingliteapp.util.StringUtils;

/**
 * Created by Genji on 2015/6/24. Modified by Kevin
 */
public class CourseNotifDao extends AbstractDao {
	public CourseNotifDao(Context context, String userId) {
		super(context, userId);
		this.tb = CourseNotifTable.TABLE_COURSE_NOTIF;
	}

	/**
	 * 通过课程id来获得该课程的所有通知
	 * 
	 * @param 课程Id
	 * @return
	 */
	public List<CourseNotifBean> getNotifByCourseId(String courseId) {
		List<CourseNotifBean> CourseNotiflist = new ArrayList<CourseNotifBean>();

		// String sql = "select * from course where userId = 123";

		Cursor cursor = db.query(this.tb, CourseNotifTable.PROJECTION, CourseNotifTable.COLUMN_COURSE_ID + "=" + StringUtils.wrapQuote(courseId), null, null, null, null);
		while (cursor.moveToNext()) {
			CourseNotiflist.add(this.makeCourseNotifBeanByCursor(cursor));
		}

		return CourseNotiflist;
	}

	/**
	 * 辅助类，用于生成实例
	 * 
	 * @param cursor
	 * @return
	 */
	private CourseNotifBean makeCourseNotifBeanByCursor(Cursor cursor) {
		CourseNotifBean CourseNotifBean = new CourseNotifBean();

		CourseNotifBean.setId(cursor.getString(cursor.getColumnIndex(CourseNotifTable.COLUMN_ID)));
		CourseNotifBean.setCreateTime(cursor.getString(cursor.getColumnIndex(CourseNotifTable.COLUMN_CREATE_TIME)));
		CourseNotifBean.setUpdateTime(cursor.getString(cursor.getColumnIndex(CourseNotifTable.COLUMN_UPDATE_TIME)));
		CourseNotifBean.setTitle(cursor.getString(cursor.getColumnIndex(CourseNotifTable.COLUMN_TITLE)));
		CourseNotifBean.setContent(cursor.getString(cursor.getColumnIndex(CourseNotifTable.COLUMN_CONTENT)));
		CourseNotifBean.setCourseId(cursor.getString(cursor.getColumnIndex(CourseNotifTable.COLUMN_COURSE_ID)));
		CourseNotifBean.setCourseName(cursor.getString(cursor.getColumnIndex(CourseNotifTable.COLUMN_COURSE_NAME)));
		// UserBean
		UserBean userBean = new UserBean();
		userBean.setId(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_ID))));
		userBean.setName(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_NAME))));
		userBean.setEmail(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_EMAIL))));
		userBean.setImageUrl(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_IMAGE_URL))));
		userBean.setNumber(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_NUMBER))));
		userBean.setPhone(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_PHONE))));
		userBean.setIsUsing(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_IS_USING))));
		userBean.setType(cursor.getString(cursor.getColumnIndex(BaseBean.captureName(UserTable.TABLE_USER,UserTable.COLUMN_TYPE))));
		CourseNotifBean.setUserBean(userBean);
		return CourseNotifBean;
	}

}
